﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ComboCol
{
    //https://www.wpf-tutorial.com/listview-control/listview-with-gridview/
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com", Categories = new List<Category> {new Category("aa") , new Category("bb"), new Category("cc") } });
            items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com", Categories = new List<Category> { new Category("aa"), new Category("bb"), new Category("cc") } });
            items.Add(new User() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com", Categories = new List<Category> { new Category("aa"), new Category("bb"), new Category("cc") } });
            lvUsers.ItemsSource = items;
        }
        public class User
        {
            public string Name { get; set; }

            public int Age { get; set; }

            public string Mail { get; set; }

            public List<Category> Categories { get; set; }
        }

        public class Category
        {
            public Category(string name)
            {
                this.Name = name;
            }
            public bool IsSelected { get; set; }
            public string Name { get; set; }
        }

        private void lvUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedItem = this.lvUsers.SelectedItem;
            if (selectedItem == null)
                return;
            User user = selectedItem as User;
            var selcted = string.Join(",",user.Categories.Where(x => x.IsSelected).Select(x => x.Name));
            this.Title=$"选中了{selcted}";
        }
    }
}
